package com.guimin.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guimin.order.constant.GmFeijiuOrderConstant;
import com.guimin.order.dao.GuiminFeijiuOrderDao;
import com.guimin.order.dto.AddRecycleDto;
import com.guimin.order.entity.GuiminFeijiuOrder;
import com.guimin.site.entity.GuiminSite;
import com.guimin.site.service.GuiminSiteService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 废旧回收订单(GuiminFeijiuOrder)表服务实现类
 *
 * @author makejava
 * @since 2021-11-25 15:28:18
 */
@Service("guiminFeijiuOrderService")
public class GuiminFeijiuOrderService extends ServiceImpl<GuiminFeijiuOrderDao, GuiminFeijiuOrder> {
    /**
     * 服务对象
     */
    @Resource
    private GuiminFeijiuOrderDao guiminFeijiuOrderDao;
    @Resource
    private GuiminSiteService guiminSiteService;

    /**
     * 获取废旧回收订单
     *
     * @param userId  用户id
     * @param current 当前页
     * @param size    每页大小
     * @return
     */
    public IPage<GuiminFeijiuOrder> getOrderListByUserId(Long userId, Integer current, Integer size) {
        return super.page(new Page<>(current, size), new LambdaQueryWrapper<GuiminFeijiuOrder>()
                .eq(GuiminFeijiuOrder::getPayerId, userId)
                .or().eq(GuiminFeijiuOrder::getPayeeId, userId)
                .orderByDesc(GuiminFeijiuOrder::getId));
    }

    /**
     * 添加废旧回收订单
     *
     * @param userId 用户id
     * @param dto    订单数据
     * @return
     */
    public boolean addRecycle(Long userId, AddRecycleDto dto) {
        //根据服务id获取回收服务
        GuiminSite service = guiminSiteService.getOne(new LambdaQueryWrapper<GuiminSite>()
                .select(GuiminSite::getId)
                .eq(GuiminSite::getId, dto.getStationId()));
        if (service==null){
            return false;
        }
        //获取付款收款账户信息


        return super.save(new GuiminFeijiuOrder()
                .setId(System.currentTimeMillis())
                //站长id
                .setAgentId(userId)
                //站点id
                .setStationId(dto.getStationId())
                .setPropertyId(33)
                .setPropertyName(service.getVillageProperty())
                .setType("")
                .setAmount(dto.getAmount())
                .setLoveEnergy(dto.getLoveEnergy())
                .setMemo(dto.getMemo())
                .setPayerId(dto.getPayerId())
                .setPayerAccount("付款人账户")
                .setPayeeId(dto.getPayeeId())
                .setPayeeAccount("收款人账户")
                .setCreateDatetime(new Date())
                .setState(GmFeijiuOrderConstant.State.BEING_PROCESSED.value)
                .setOrderDescribe("订单描述")
        );
    }
}

